package com.frost.blog.config;

import com.frost.blog.util.JWTUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import static com.frost.blog.constants.Constant.USER_TOKEN;

// 拦截器
@Component
public class LoginInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        // 1.从header中获取token
        // 2.校验token
        // 3.放行
        String userToken = request.getHeader(USER_TOKEN);
        boolean result = JWTUtils.checkToken(userToken);
        if (result) {
            return true;
        }
        response.setStatus(401);
        return false;
    }

}
